<template>
	<view class="work-container">
	<view class="draw-btn-left" @tap="onShowShare">
		<view class="btn-item">
			分享好友赚现金
		</view>
	</view>
	<!-- 分享弹窗 -->
	<view class="promo-share" v-if="shareShow">
		<view class="promo-main">
			<view class="promo-icons-close" @tap="closeEarn">
				<image src="/static/images/icon/close.png"></image>
			</view>
			<view class="promo-tit">
				<title>立即分享给好友</title>
			</view>
			<view class="promo-icons">
				<!-- #ifdef H5  -->
				<button class="promo-img1" @tap="onShareWay" open-type="share" v-if="isWechat"><!-- 微信环境下的H5 -->
					<image src="/static/images/icon/weixin.png"></image>微信
				</button>
				<!-- #endif -->
				<!-- #ifdef MP-WEIXIN  -->
				<button class="promo-img1" open-type="share" v-if="isWechat">
					<image src="/static/images/icon/weixin.png"></image>微信
				</button>
				<!-- #endif -->
			</view>
		</view>
	</view>
	<!-- 分享弹窗 end -->
	</view>
</template>
<script>
	export default {
		data() {
			return {
				userId: wx.getStorageSync('loginResult').userId,
				inviteBg: '',
				//分享
				isWechat: false, //是否为微信环境
				shareShow: false, //分享弹窗是否显示
				path: '', // 链接
			};
		},
		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function(options) {
			// #ifdef H5
			this.isWechat = false
			// #endif
			// #ifdef APP-PLUS
			this.isWechat = false
			// #endif
			// #ifdef MP-WEIXIN
			this.isWechat = true
			// #endif
		},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function(res) {
			if (res.from === 'button') {
				var shareId = wx.getStorageSync('loginResult').userId;
				return {
					title: "邀请好友，领现金红包",
					path: '/pages/login/login?shareId=' + shareId,
					imageUrl: this.inviteBg, //数据返回的图片数据
					success: function(res) {
						// 转发成功
						console.log(res)
					},
					fail: function(res) {
						// 转发失败
						console.log(res)
					}
				}
			} else { //用户点击右上角3个点分享（小程序端）
				return {
					path: "pages/index/index"
				};
			}
		},

		methods: {
			/***生成复制分享链接*/
			getShareLink: function() {
				//从缓存获取当前分享者id（当前登录用户的id:登录成功后返回的信息存到缓存里），以备转发时使用
				var shareId = wx.getStorageSync('loginResult').userId;
				this.path = '/pages/login/login?shareId=' + shareId
			},
			/*** 显示分享弹窗*/
			onShowShare: function(e) {
				// this.setData({
				// 	shareShow: true
				// });
				this.shareShow=true;
				this.getShareLink()
				//从缓存获取分享者id，如果没有则去接口获取，以备转发时使用
				var shareId = wx.getStorageSync('loginResult').userId;
			},

			/**
			 * 关闭分享弹窗
			 */
			closeEarn: function() {
				this.shareShow=false;
			},
		}
	}
</script>

<style>
	.ipt {
		display: block;
		width: 78%;
		margin: 30rpx auto;
		height: 60rpx;
		padding-left: 20rpx;
		color: #333;
		border: 2rpx solid #333;
	}
</style>